<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-01-17
 * 版    本：1.0.0
 * 功能说明：后台登录控制器。
 *
 **/

namespace Admin\Controller;

use Think\Controller;

class AdminUserController extends CommonController
{
    public function index(){
        $info = M('Administrator')->where('status=1')->select();
        $AdminUser='active';
        $ptitle='后台用户管理';
        $title='首页';
        $this->assign('ptitle',$ptitle);
        $this->assign('title',$title);
        $this->assign('AdminUser',$AdminUser);
        $this->assign('info',$info);
        $this->display();
    }

    public function add()
    {
        $AdminUser='active';
        $ptitle='后台用户管理';
        $title='添加';
        $this->assign('ptitle',$ptitle);
        $this->assign('title',$title);
        $this->assign('AdminUser',$AdminUser);
        $this->display();
    }
    
    public function doadd()
    {
        $info['username'] = I('REQUEST.username','','trim,htmlspecialchars');
        $password = I('REQUEST.password','','trim,htmlspecialchars');
        $info['pwd'] = md5($password);

        
        //var_export($info);exit;
        if(M('Administrator')->add($info)){
            $this ->ajaxReturn(array('info'=>'保存成功！','status'=>1));
        }else{
            $this ->ajaxReturn(array('info'=>'保存失败！','status'=>0));
        }
    }

    public function del()
    {

        $uids = isset($_REQUEST['uids']) ? $_REQUEST['uids'] : false;
        //uid为1的禁止删除
        if ($uids == 1 or !$uids) {
            $this->error('参数错误！');
        }
        if (is_array($uids)) {
            foreach ($uids as $k => $v) {
                if ($v == 1) {//uid为1的禁止删除
                    unset($uids[$k]);
                }
                $uids[$k] = intval($v);
            }
            if (!$uids) {
                $this->error('参数错误！');
                $uids = implode(',', $uids);
            }
        }

        $map = array('id'=>$uids);
        if (M('Administrator')->where($map)->delete()) {
            $this->success('恭喜，用户删除成功！');
        } else {
            $this->error('参数错误！');
        }
    }

    public function edit()
    {

        $id = isset($_GET['id']) ? intval($_GET['id']) : false;
        if($id==1 && $id!=$_SESSION['uid']){
            $this->error('对不起,您没有权限!');
        }
        if (!$id) {
            $this->error('参数错误！');
        }
        $info = M('Administrator')->where('id='.$id)->find();
        $AdminUser='active';
        $ptitle='后台用户管理';
        $title='修改';
        $this->assign('ptitle',$ptitle);
        $this->assign('title',$title);
        $this->assign('AdminUser',$AdminUser);
        $this->assign('info',$info);
        $this->display();
    }

    public function doupdate()
    {
        $id = I('REQUEST.id','','int');
        if(!$id){
            $this->ajaxReturn(array('info'=>'参数传递错误！','status'=>0));
        }
        $password = I('REQUEST.password','','trim,htmlspecialchars');
        $info['pwd'] = md5($password);
        $info['lasttime'] = time();
        if(M('Administrator')->where('id='.$id)->data($info)->save()){
            $this ->ajaxReturn(array('info'=>'修改成功！','status'=>1));
        }else{
            $this ->ajaxReturn(array('info'=>'修改失败！','status'=>0));
        }
    }

}
